package com.snail.readyvr.ui;

import android.content.Context;
import android.content.res.ColorStateList;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.util.DensityUtils;

/**
 * Created by MRKING on 2016/9/23.
 */

public class IndicatorTiTle extends FrameLayout {
    private Context mContext;
    private String[] mTitls;
    private SparseArray<TextView> mTitleViews;
    private SparseArray<View> mUnderLines;
    private OnClickTitle mOnClickTitle;
    private LinearLayout mTitleContent;
    private LinearLayout.LayoutParams mLayoutParams;
    private ColorStateList mSelectTextcolor;// 被选中时的文字颜色
    private ColorStateList mNormalTextcolor;// 未被被选中时的文字颜色
    private int mLastPosition = 0;

    public IndicatorTiTle(Context context) {
        this(context,null);
    }

    public IndicatorTiTle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndicatorTiTle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        mTitleViews = new SparseArray<>();
        mUnderLines = new SparseArray<>();
        mSelectTextcolor = mContext.getResources().getColorStateList(R.color.indicator_title_selected_color);
        mNormalTextcolor = mContext.getResources().getColorStateList(R.color.indicator_title_normal_color);
        View view = LayoutInflater.from(mContext).inflate(R.layout.indicator_layout,null);
        mTitleContent = (LinearLayout) view.findViewById(R.id.indicator_linearlayout);

        addView(view);
    }

    public interface OnClickTitle{
        void onClickTitle(int position);
    }

    public void setTitls(String[] titls) {
        mTitls = titls;
        initTitle();
        changeTitleState(0);
    }

    private void initTitle() {
        for (int i=0;i < mTitls.length;i++){
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.indicator_item_layout,null);
            TextView text = (TextView) itemView.findViewById(R.id.indicator_title);
            text.setText(mTitls[i]);
            mTitleViews.put(i,text);
            View underLine = itemView.findViewById(R.id.indicator_title_underline);
            mUnderLines.put(i,underLine);

            mTitleContent.addView(itemView);
            mLayoutParams = (LinearLayout.LayoutParams) itemView.getLayoutParams();

            if(mTitls.length == 1){
                mLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                mLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                itemView.setLayoutParams(mLayoutParams);
            }else {
                final int position = i;
                text.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mOnClickTitle != null) {
                            if(position == mLastPosition)
                                return;
                            mLastPosition = position;
                            mOnClickTitle.onClickTitle(position);
                        }
                        changeTitleState(position);
                    }
                });
                mLayoutParams.width = 0;
                mLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                mLayoutParams.weight = 1;
                itemView.setLayoutParams(mLayoutParams);
                mTitleContent.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
                mTitleContent.setDividerDrawable(mContext.getResources().getDrawable(R.drawable.indicator_item_divider_bg));
                mTitleContent.setDividerPadding(DensityUtils.dp2px(mContext,18));

            }


        }
    }

    private void changeTitleState(int position) {
        for (int i=0;i < mTitleViews.size();i++){
           if(position == i){
               mTitleViews.get(i).setTextColor(mSelectTextcolor);
               mUnderLines.get(i).setVisibility(View.VISIBLE);
           }else {
               mTitleViews.get(i).setTextColor(mNormalTextcolor);
               mUnderLines.get(i).setVisibility(View.GONE);
           }
        }
    }
    public void setOnClickTitle(OnClickTitle onClickTitle) {
        mOnClickTitle = onClickTitle;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (int i=0;i < mTitleViews.size();i++){

                mTitleViews.get(i).setEnabled(enabled);

        }

    }
}
